var api = require("../../api.js"),
  app = getApp();
import {
  $Login
} from '../../components/base.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    compId: '',
    communityData: [],
    companyData: {},
    descHeight: 'auto',
    isHiddenDesc: false,
    invite_open_id: '',
    personnel: null
  },
  tell: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.companyData.phone //仅为示例，并非真实的电话号码
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.compId)
    this.setData({
      compId: options.compId,
      invite_open_id: options.invite_open_id || ''
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showLoading({
      title: "数据加载中"
    });
    /** 是否已授权 */
    getApp().is_empower();
    setTimeout(() => {
      let data
      let that = this;
      $Login();
      if (this.data.invite_open_id && this.data.invite_open_id !== undefined) {
        data = {
          id: this.data.compId,
          open_id: app.globalData.wxUserInfo.openId,
          invite_open_id: this.data.invite_open_id
        }
      } else {
        data = {
          id: this.data.compId,
          open_id: app.globalData.wxUserInfo.openId
        }
      }
      wx.request({
        url: api.company.mineCompany,
        data: data,
        header: {
          "content-type": "application/json"
        },
        method: 'get',
        success(res) {
          setTimeout(() => {
            console.log('我的公司接口返回',res.data.data)
            const content = res.data.data.company.detailDesc;
            res.data.data.company.detailDesc = res.data.data.company.detailDesc ? that.formatRichText(content) : '';
            if (res.data.data.personnel != null) {
              that.setData({
                personnel: res.data.data.personnel
              })
            }
            that.setData({
              communityData: res.data.data.communities,
              companyData: res.data.data.company,
            }, function() {
              wx.setNavigationBarTitle({
                title: that.data.companyData.name
              })
              that.hiddenDesc()
            })
          }, 500)
          wx.hideLoading()
        }
      })
    }, 800)
  },
  changeIsHiddenDesc: function() {
    this.setData({
      descHeight: 'auto',
      isHiddenDesc: false
    })
  },
  formatRichText: function(html) {
    let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
    return newContent;
  },
  hiddenDesc: function() {
    let that = this
    var query = wx.createSelectorQuery();
    query.select('.desc').boundingClientRect(function(rect) {
      // console.log(rect.height)
      if (rect.height < 200) {
        that.setData({
          isHiddenDesc: false,
          descHeight: 'auto'
        })
      } else {
        that.setData({
          isHiddenDesc: true,
          descHeight: '200rpx'
        })
      }
    }).exec();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  // toMap: function () {
  //   wx.navigateTo({
  //     url: `../map/map?mapData=${this.data.companyData.address}`
  //   })
  // },
  toCommunities: function(e) {
    const communitiesid = e.currentTarget.dataset.communitiesid
    wx.navigateTo({
      url: `../community/community?communitiesId=${communitiesid}`
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(type) {
    return {
      title: this.data.companyData.name,
      path: `pages/mineCompany/mineCompany?compId=${this.data.compId}&invite_open_id=${app.globalData.wxUserInfo.openId || ''}`,
      success: function(res) {},
      fail: function(res) {}
    }
  }
})